package com.newworld.dmsapp2;
import java.text.SimpleDateFormat;
import java.util.Date;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;

/**
 * Receive the broadcast and start the activity that will show the alarm
 */
public class MyReceiver extends BroadcastReceiver {

    /**
     * called when the BroadcastReceiver is receiving an Intent broadcast.
     */
	/*这里notification_id最好是云吧传的信息里的repiar_id;*/
/*	static Random random = new Random();
	int notification_id = random.nextInt();*/
	NotificationManager nm;
	
	Intent intent1 = new Intent();
	public static int notification_remind = DemoReceiver.notification_id;
    @Override
    public void onReceive(Context context, Intent intent) {
        
        /* start another activity - MyAlarm to display the alarm */
    	
    	/*NotificationManager manager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
        //构建一个通知对象(需要传递的参数有三个,分别是图标,标题和 时间)
        Notification notification = new Notification(R.drawable.ic_launcher,"通知",System.currentTimeMillis());
        //Intent intent = new Intent(context,context);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.setClass(context, MyAlarm.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(context,0,intent,0);     
        notification.setLatestEventInfo(context, "通知标题", "通知显示的内容", pendingIntent);
        notification.flags = Notification.FLAG_AUTO_CANCEL;//点击后自动消失
        notification.defaults = Notification.DEFAULT_SOUND;//声音默认
        manager.notify(0, notification);//发动通知,id由自己指定，每一个Notification对应的唯一标志
        //其实这里的id没有必要设置,只是为了下面要用到它才进行了设置
    	String msg = intent.getStringExtra("msg") + transferLongToDate("yyyy-MM-dd HH:mm:ss", System.currentTimeMillis());
		Log.i("SendReceiver", msg);  
		Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
        context.startActivity(intent);*/
    	//showNotification(context,R.drawable.ic_launcher, "图标边的文字", "标题", "内容");
    	if(DemoReceiver.count!=0){
    		//String msg = intent.getStringExtra("msg") + transferLongToDate("yyyy-MM-dd HH:mm:ss", System.currentTimeMillis());
    		String msg = "你有"+DemoReceiver.count+"条新任务" + transferLongToDate("yyyy-MM-dd HH:mm:ss", System.currentTimeMillis());
    		Log.i("SendReceiver", msg);  
    		//Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
    	}
    }

	/**
     * 把毫秒转化成日期
     * @param dateFormat(日期格式，例如：MM/ dd/yyyy HH:mm:ss)
     * @param millSec(毫秒数)
     * @return
     */
    private String transferLongToDate(String dateFormat,Long millSec){
    	SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
    	Date date= new Date(millSec);
    	return sdf.format(date);
    }
    
    @SuppressWarnings("deprecation")
	public void showNotification(Context context,int icon, String tickertext, String title,
			String content) {
		// 设置一个唯一的ID，随便设置

		// Notification管理器
		Notification notification = new Notification(icon, tickertext,
				System.currentTimeMillis());
		// 后面的参数分别是显示在顶部通知栏的小图标，小图标旁的文字（短暂显示，自动消失）系统当前时间（不明白这个有什么用）
		notification.defaults = Notification.DEFAULT_ALL;
		// 这是设置通知是否同时播放声音或振动，声音为Notification.DEFAULT_SOUND
		// 振动为Notification.DEFAULT_VIBRATE;
		// Light为Notification.DEFAULT_LIGHTS，在我的Milestone上好像没什么反应
		// 全部为Notification.DEFAULT_ALL
		// 如果是振动或者全部，必须在AndroidManifest.xml加入振动权限
		PendingIntent pt = PendingIntent.getActivity(context, 0, new Intent(context,
				DemoReceiver.class), 0);
		// 点击通知后的动作，这里是转回main 这个Acticity
		notification.setLatestEventInfo(context, title, content, pt);
		nm.notify(notification_remind, notification);

	}

}